package cn.flying.cloud.system.area.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.flying.cloud.base.common.utils.Ft;
import cn.flying.cloud.base.common.vo.PageRequest;
import cn.flying.cloud.base.common.vo.PageResponse;
import cn.flying.cloud.base.common.vo.vue.CascadeInfoVo;
import cn.flying.cloud.system.area.entity.SysAreaInfo;
import cn.flying.cloud.system.area.mapper.SysAreaInfoMapper;
import cn.flying.cloud.system.area.service.SysAreaInfoService;

/**
 * 行政区域编码信息表 服务实现类
 *
 * @author admin
 * @since 2025-08-13 09:37:07
 */
@Service
public class SysAreaInfoServiceImpl extends ServiceImpl<SysAreaInfoMapper, SysAreaInfo> implements SysAreaInfoService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 查询总数
     *
     * @param param
     * @return
     */
    @Override
    public long countObj(SysAreaInfo param) {
        return this.count(getLambdaQueryWrapper(param));
    }

    /**
     * 分页查询行政区域编码信息表数据
     *
     * @param request
     * @return
     */
    @Override
    public PageResponse<SysAreaInfo> getPageList(PageRequest<SysAreaInfo> request) {
        SysAreaInfo param = request.getParam();
        LambdaQueryWrapper<SysAreaInfo> wrapper = getLambdaQueryWrapper(param);
        // wrapper TODO
        Page<SysAreaInfo> page = this.page(new Page<>(request.getPageNum(), request.getPageSize()), wrapper);
        return new PageResponse<>(page.getTotal(), (int) page.getCurrent(), page.getRecords());
    }

    /**
     * 构建查询对象
     *
     * @param param
     * @return
     */
    private LambdaQueryWrapper<SysAreaInfo> getLambdaQueryWrapper(SysAreaInfo param) {
        LambdaQueryWrapper<SysAreaInfo> wrapper = new LambdaQueryWrapper<>();
        if (param != null) {
            if (Ft.String.isNotBlank(param.getCode())) {
                wrapper.eq(SysAreaInfo::getCode, param.getCode());
            }
            if (Ft.String.isNotBlank(param.getName())) {
                wrapper.eq(SysAreaInfo::getName, param.getName());
            }
            if (Ft.String.isNotBlank(param.getParentCode())) {
                wrapper.eq(SysAreaInfo::getParentCode, param.getParentCode());
            }
            if (Ft.String.isNotBlank(param.getLevel())) {
                wrapper.eq(SysAreaInfo::getLevel, param.getLevel());
            }
//        if (Ft.String.isNotBlank(param.getStatus())) {
//            wrapper.eq(SysAreaInfo::getStatus, param.getStatus());
//        }
        }
        return wrapper;
    }

    /**
     * 列表查询行政区域编码信息表数据
     *
     * @param param
     * @return
     */
    @Override
    public List<SysAreaInfo> getList(SysAreaInfo param) {
        LambdaQueryWrapper<SysAreaInfo> wrapper = getLambdaQueryWrapper(param);
        // wrapper TODO
        return this.list(wrapper);
    }

    /**
     * 查询行政区域编码信息表详情数据
     *
     * @param param
     * @return
     */
    @Override
    public SysAreaInfo getOneObj(SysAreaInfo param) {
        LambdaQueryWrapper<SysAreaInfo> wrapper = new LambdaQueryWrapper<>();
        // wrapper TODO
        return this.getOne(wrapper);
    }

    /**
     * 根据Id查询行政区域编码信息表详情数据
     *
     * @param id
     * @return
     */
    @Override
    public SysAreaInfo getObjById(Serializable id) {
        return this.getById(id);
    }

    /**
     * 新增行政区域编码信息表数据
     *
     * @param param
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean createObj(SysAreaInfo param) {
        param.setTenantId(Ft.Local.TenantId());
        return this.save(param);
    }

    /**
     * 更新行政区域编码信息表数据
     *
     * @param param
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateObj(SysAreaInfo param) {
        return this.saveOrUpdate(param);
    }

    /**
     * 删除行政区域编码信息表数据
     *
     * @param param
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteObj(SysAreaInfo param) {
        LambdaQueryWrapper<SysAreaInfo> wrapper = new LambdaQueryWrapper<>();
        // wrapper TODO
        return this.remove(wrapper);
    }

    /**
     * 根据Id删除行政区域编码信息表数据
     *
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteById(Serializable id) {
        return this.removeById(id);
    }

    /**
     * 获取行政区域级联下拉组件数据
     *
     * @return
     */
    @Override
    public List<CascadeInfoVo> getCascaderInfoList() {
        LambdaQueryWrapper<SysAreaInfo> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(SysAreaInfo::ge, "Y");
        List<SysAreaInfo> list = this.list(wrapper);
        Map<String, List<SysAreaInfo>> listMap = list.stream().collect(Collectors.groupingBy(SysAreaInfo::getParentCode));
        List<SysAreaInfo> roots = listMap.get("0");
        return setChildren(roots, listMap);
    }

    private List<CascadeInfoVo> setChildren(List<SysAreaInfo> list, Map<String, List<SysAreaInfo>> listMap) {
        List<CascadeInfoVo> infoVos = new ArrayList<>();
        list.forEach(r -> {
            CascadeInfoVo vo = new CascadeInfoVo();
            vo.setValue(r.getCode());
            vo.setLabel(r.getName());

            List<SysAreaInfo> children = listMap.get(r.getCode());
            if (!CollectionUtils.isEmpty(children)) {
                vo.setChildren(setChildren(children, listMap));
            }
            infoVos.add(vo);
        });
        return infoVos;
    }
}
